Vue.component("friend_list", {
	props : [ 'rightCurrContent', 'account' ],
	data : function() {
		var data = {
				search_content:[],
				menu_items:[
				            {
				            	menu_item:"删除",
	
				            }
				            ],
			comp_ident : null,// 组件标识：contact:全部好友，，，recent:最近联系人
			right_menu_show:false,
			friends : [

			],
			itemSelected : null,
			
		}
		return data;
	},
	watch:{
		search_content:function(){
			
			}
		},
	ready : function() {
		
		// 获取好友列表数据
		// 匹配到的搜索内容
//		var content = [{
//			title: 'dsd'
//		},
//		
//
//		];
//		
		
	//	var content=this.search_content;
		
		
		
		// 搜索框动态效果
		
		
	},
	template : loadHTML("components/friendlist_comp/friendlist_comp.html"),
	methods : {
		admin_fri:function(action){
			console.log(action);
		
			if(action=="删除聊天"){
				//删除选择的最近联系人
				var type="";
				if(this.friends[this.itemSelected].type=="[群组]"){
					type="group";
				}else{
					type="user";
				}
				sendRequest("delete_recent_chat", "post", "json", false, {account:this.account,friaccount:this.friends[this.itemSelected].friaccount,type:type}, function(res){
					if(res.success){
						console.log("删除成功");
						this.friends.splice(this.itemSelected,1);
						
					}
					this.right_menu_show=false;
					$(".right_menu.show").css("display","none");
				}.bind(this));
			}
			if(action=="退出群"){
				//删除群成员
				sendRequest("delete_remeber_by_user","post","json",false,{account:this.account,group_id:this.friends[this.itemSelected].friaccount},function(res){
					if(res.success){
						console.log("删除成功");
						this.friends.splice(this.itemSelected,1);
						
					}
					this.right_menu_show=false;
					$(".right_menu.show").css("display","none");
				}.bind(this));
			}
			if(action=="删除好友"){
				//删除好友
				sendRequest("delete_contact", "post","json", false, {account:this.account,friaccount:this.friends[this.itemSelected].friaccount}, function(res){
					if(res.success){
						console.log("删除成功");
						this.$emit("delete_recent",this.friends[this.itemSelected].friaccount);
						this.friends.splice(this.itemSelected,1);
						
						
						
					}
					this.right_menu_show=false;
					$(".right_menu.show").css("display","none");
				}.bind(this));
				
			}
		},
		delete_recent:function(friaccount){
			for(var i=0;i<this.friends.length;i++){
				if(this.friends[i].friaccount==friaccount){
					this.friends.splice(i,1);
				}
			}
		},
		mousedown:function(a){
			
			var event=a.$event;
			if(event.which==3){
				this.menu_items=[];
				if(this.friends[a.$index].type=="[群组]"){
					this.menu_items.push({
						menu_item:"退出群",
					},
					{menu_item:"删除聊天"}
					);
				}else{
					if(this.comp_ident=="recent"){
						this.menu_items.push(
								{menu_item:"删除聊天"}
								);
					}
					if(this.comp_ident=="contact"){
						this.menu_items.push(
								{menu_item:"删除好友"}
								);
					}
					
					
				}
				console.log("鼠标右键按下");
				this.itemSelected=a.$index;
				this.right_menu_show=true;
				$(".right_menu.show").css("top",event.pageY);
				$(".right_menu.show").css("left",event.pageX);
				$(".right_menu.show").css("display","block");
				
				
			}else{
			
				
				this.right_menu_show=false;
				$(".right_menu.show").css("display","none");
				
			}
		},
		item_select_by_account : function(account) {
			for (var i = 0; i < this.friends.length; i++) {
				if (account == this.friends[i].friaccount) {
					this.itemSelected = i;
				}
			}
		},
		itemClick : function(a) {

			if (a == null) {
				this.itemSelected = null;
				return;
			}
			if (a != null) {
				this.$refs.system_msg_title_comp.selected = false;
			}
			this.itemSelected = a.$index;
			if (this.comp_ident == "contact") {
				var data = {
					show_page : "userinfo_comp",
					opts : {
						btn_msg : "发送消息",
						friaccount : this.friends[a.$index].friaccount
					}

				};
				this.$emit("curr_right_content", data);
			}
			if (this.comp_ident == "recent") {
				var data = {
					show_page : "chat_panel_comp",
					opts : {
						chat_type : "single",// 群聊还是单聊？
						title : this.friends[a.$index].username,
						friaccount : this.friends[a.$index].friaccount,
						username : this.friends[a.$index].username,

					}
				};
				if (this.friends[a.$index].type == "[群组]") {
					data.opts.chat_type = "group";

				}else{
					this.friends[a.$index].num=0;
					this.friends[a.$index].num_is_show=false;
					this.friends[a.$index].num_is_hidden=true;
					sendRequest("updateAllNoReadMsgState", "post", "json", true, {account:this.account,friaccount:this.friends[a.$index].friaccount}, function(res){
						
					})
				}
				this.$emit("curr_right_content", data);
			}
		},

		init : function(flag, param) {
			// 初始化列表数据:flag:列表标识，，param：参数
			console.log("显示：" + flag);
			if (flag == "recent") {
				sendRequest("getFriList_" + flag, "post", "json", true, param,
						function(data) {
							console.log(data.success);
							var arr = [];
							for (var i = 0; i < data.data.length; i++) {
								if (data.data[i].type == "user") {
									arr.push({
										"username" : data.data[i].name,
										"account" : data.data[i].tip,
										type : "",
										"fri_icon" : "get_icon?account="
												+ data.data[i].tip + "&code="
												+ 100 * Math.random(),
										"num":0,
										"time":null,
										"num_is_show":false,
										"num_is_hidden":true
									});
								
									
								}
								if (data.data[i].type == "group") {
									arr.push({
										"username" : data.data[i].name,
										"account" : data.data[i].tip,
										type : "[群组]",
										"fri_icon" : "images/group.png",
										"num":0,
										"time":null
									});

								}

							}
							this.init_data(arr);
							
							
							
							
							
							for(var i=0;i<this.friends.length;i++){
								if(this.friends[i].num==0){
									this.friends[i].num_is_show=false;
									this.friends[i].num_is_hidden=true;
								}else{
									this.friends[i].num_is_show=true;
									this.friends[i].num_is_hidden=false;
								}
							}
						}.bind(this));
			}
			if (flag == "contact") {
				sendRequest("getFriList_" + flag, "post", "json", true, param,
						function(data) {
							if (data.data.length != 0) {
								console.log(data);
								// this.friends=[];
								var arr = [];
								for (var i = 0; i < data.data.length; i++) {
									arr.push({
										"username" : data.data[i][1],
										"account" : data.data[i][0],
										"type" : "",
										"fri_icon" : "get_icon?account="
												+ data.data[i][0] + "&code="
												+ 100 * Math.random()
									});
								}
								this.init_data(arr);
							}

						}.bind(this));
			}

		},
		set_right_panel : function(curr) {
			// console.log("set_right_panel----friendlist:"+curr);
			this.$emit("curr_right_content", curr);
		},
		add_data : function(fri) {
			
			if (this.comp_ident == "recent") {

				if (fri.type == "[群组]") {
					this.friends.push({
						"username" : fri.username,
						"friaccount" : fri.account,
						"type" : fri.type,
						"icon_src" : "images/group.png",
						"msg_box" : [],
					});
				} else {
					this.friends.push({
						"username" : fri.username,
						"friaccount" : fri.account,
						"type" : "",
						"icon_src" : fri.fri_icon,
						"msg_box" : [],
						"num":0,
						"time":null,
						"num_is_show":false,
						"num_is_hidden":true
					});
					
					
				}

			}else{
				this.friends.push({
					friaccount:fri.account,
					username:fri.username,
					icon_src:"get_icon?account="
						+ fri.account + "&code="
						+ 100 * Math.random(),
					type:null
				});
			}

		},
		init_data : function(frilist) {// 数据更新接口
			console.log("------friendlist------");
			console.log(frilist);
			this.friends = [];

			for (var i = 0; i < frilist.length; i++) {
				if (this.comp_ident == "recent") {

					if (frilist[i].type == "[群组]") {
						this.friends.push({
							"username" : frilist[i].username,
							"friaccount" : frilist[i].account,
							"type" : frilist[i].type,
							"icon_src" : "images/group.png",
							"msg_box" : [],
							"time":frilist[i].time,
							"num":frilist[i].num
						});
					} else {
						//获取所有未读消息
						var msg_box_=[];
						var num_is_show=false;
						var num_is_hidden=true;
				
						this.friends.push({
							"username" : frilist[i].username,
							"friaccount" : frilist[i].account,
							"type" : frilist[i].type,
							"icon_src" : frilist[i].fri_icon,
							"msg_box" : [],
							"time":frilist[i].time,
							"num":frilist[i].num,
							"num_is_show":false,
							"num_is_hidden":true
						});	
					}
					//continue;
				}else{
					this.friends.push({
						"username" : frilist[i].username,
						"friaccount" : frilist[i].account,
						"type" : frilist[i].type,
						"icon_src" : frilist[i].fri_icon
					});
				}
			
			}
			
			var search_content=[];
			for(var i=0;i<this.friends.length;i++){
				search_content.push(
						{
							title:this.friends[i].friaccount,
							title:this.friends[i].username,
						}
						);
			}
			
			$('.contact_searchbar .ui.search').search({
				source:search_content,
				error: {
					noResults: '找不到匹配的结果'
				}

			});
			
			if(this.comp_ident=="contact"){
				return;
			} 
			
		
			this.getNoReadData();


		},
		getNoReadData:function(){
			//读取未读消息
			sendRequest("getNoReadMsg", "post", "json",true, {account:this.account}, function(res){
				if(res.success){
					for(var j=0;j<res.data.length;j++){
						var time_=res.data[j].time;
						//var changeTime=time_.replace(time_.substring(12,14),parseInt(time_.substring(12,14))+12);
						var msgobj={
								msg_type:"user",
								fri_account:res.data[j].account,
								opts:{
									account:res.data[j].account,
									time:time_.substring(12,17),
									type:res.data[j].type,
									msg:res.data[j].msg
								}
						}
						
						for(var k=0;k<this.friends.length;k++){
							if(this.friends[k].friaccount==res.data[j].account){
								this.add_msg(msgobj);
								this.friends[k].num=res.data.length;
								this.friends[k].time=msgobj.opts.time;
								this.friends[k].num_is_show=true;
								this.friends[k].num_is_hidden=false;
							}
						}
					}
				}
				
			}.bind(this));
			
		},
		is_exit : function(param) {// 判断指定角色是否存在于当前列表里

			for (var i = 0; i < this.friends.length; i++) {
				if (param == this.friends[i].friaccount) {
					this.auto_select(param);
					return true;
				}
			}
			return false;
		},
		id_exit_noselect : function(param) {
			for (var i = 0; i < this.friends.length; i++) {
				if (param == this.friends[i].friaccount) {

					return true;
				}
			}
			return false;
		},
		auto_select : function(param) {
			// 自动选择聊天对象：param:用户账号或者群组id
			for (var i = 0; i < this.friends.length; i++) {
				if (param == this.friends[i].friaccount) {
					this.itemSelected = i;
				}
			}

		},
		add_msg : function(data) {
			if(data.msg_type=="group"){
				for (var i = 0; i < this.friends.length; i++) {
					if (this.friends[i].friaccount == data.group_id
							&& this.friends[i].type == "[群组]") {
						this.friends[i].msg_box.push(data.opts);
						//this.friends[i].num+=1;
						
					}
				}
			}else{
				for (var i = 0; i < this.friends.length; i++) {
					if (this.friends[i].friaccount == data.fri_account
							&& this.friends[i].type == "") {
						
						this.friends[i].msg_box.push(data.opts);
					}
				}
			}
			
		},
		get_msg : function(account) {
			for (var i = 0; i < this.friends.length; i++) {
				if (this.friends[i].friaccount == account) {
					// console.log(this.friends[i].msg_box);

					return JSON.parse(JSON.stringify(this.friends[i].msg_box));
				}
			}
			return "null";
		},
		set_time:function(tip,time){
			for(var i=0;i<this.friends.length;i++){
				if(this.friends[i].friaccount==tip){
					this.friends[i].time=time;
				}
			}
		},
		set_num:function(tip){
			for(var i=0;i<this.friends.length;i++){
				if(this.friends[i].friaccount==tip){
					this.friends[i].num+=1;
					this.friends[i].num_is_show=true;
					this.friends[i].num_is_hidden=false;
				}
			}
		},
		clear_num:function(tip){
			for(var i=0;i<this.friends.length;i++){
				if(this.friends[i].friaccount==tip){
					this.friends[i].num=0;
				
							this.friends[i].num_is_show=false;
							this.friends[i].num_is_hidden=true;
						
					
				}
			}
		},
		get_all_contacts:function(){
			return JSON.parse(JSON.stringify(this.friends));
		}
		,get_recents:function(){
			return JSON.parse(JSON.stringify(this.friends));
		},
		

	}

});